/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.adapter.OnDateDelayAdapter;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.global.icu.text.SimpleDateFormat;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * DateDelaySlice
 *
 * @since 2021-03-29
 */
public class DateDelaySlice extends BaseAbilitySlice {
    TimetableView mTimetableView;

    Text titleTextView;
    List<MySubject> mySubjects;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_date_delay);
        mySubjects = SubjectRepertory.loadDefaultSubjects();
        titleTextView = (Text) findComponentById(ResourceTable.Id_id_title);
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        MaterialSpinner spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        spinner.setShowFalse(true);
        initTimetableView();
        data();
        intents = intent;
    }

    private void data() {
        int cur = mTimetableView.curWeek();
        mTimetableView.onDateBuildListener().onUpdateDate(cur, cur);

        OnDateDelayAdapter adapter = (OnDateDelayAdapter) mTimetableView.onDateBuildListener();
        long when = adapter.whenBeginSchool();
        if (when > 0) {
            titleTextView.setText("距离开学还有" + when + "天");
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);

        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .maxSlideItem(10)
                .callback(getDateDelayAdapter())
                .callback(new ISchedule.OnWeekChangedListener() {
                    @Override
                    public void onWeekChanged(int curWeek) {
                        if (mTimetableView.onDateBuildListener() instanceof OnDateDelayAdapter) {
                            OnDateDelayAdapter adapter = (OnDateDelayAdapter) mTimetableView.onDateBuildListener();
                            long when = adapter.whenBeginSchool();
                            if (when > 0) {
                                titleTextView.setText("距离开学还有" + when + "天");
                            } else {
                                titleTextView.setText("第" + curWeek + "周");
                            }
                        }
                    }
                })
                .showView();
    }

    /**
     * 配置OnDateDelayAdapter
     *
     * @return OnDateDelayAdapter
     */
    public OnDateDelayAdapter getDateDelayAdapter() {
        OnDateDelayAdapter onDateDelayAdapter = new OnDateDelayAdapter();

        /**
         * 计算开学时间戳
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long startTime = 0;
        try {
            startTime = sdf.parse("2022-09-03 00:00").getTime();
        } catch (ParseException e) {
            String ex = e.toString();
        }

        /**
         * 计算开学时的一周日期，我这里模拟一下
         */
        List<String> dateList = Arrays.asList("9", "03", "04", "05", "06", "07", "08", "09");

        onDateDelayAdapter.setStartTime(startTime);
        onDateDelayAdapter.setDateList(dateList);
        return onDateDelayAdapter;
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
